﻿using System;
using System.Windows.Forms;
using X18.Framework.Core.Security;
using X18.Framework.Core.Utils;

namespace X18.Tools.Pwd
{
    public partial class Pwd : Form
    {
        public Pwd()
        {
            InitializeComponent();
        }

        private void Pwd_Load(object sender, EventArgs e)
        {
            EncryptedType.SelectedIndex = 3;
        }

        private void Generate_Click(object sender, EventArgs e)
        {
            try
            {
                var key = "";
                //
                var encryptedType = EncryptedType.SelectedIndex;
                switch (encryptedType)
                {
                    case 0:
                        key = DataEncryptor.GenerateKey(DataEncryptorType.Normal);
                        break;
                    case 1:
                        key = DataEncryptor.GenerateKey(DataEncryptorType.SM4);
                        break;
                    case 2:
                        key = DataEncryptor.GenerateKey(DataEncryptorType.SM2);
                        break;
                    case 3:
                        key = DataEncryptor.GenerateKey(DataEncryptorType.AES);
                        break;
                    case 4:
                        key = DataEncryptor.GenerateKey(DataEncryptorType.RSA);
                        break;
                }
                SecretKey.Text = key;
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                if (ex.InnerException != null)
                {
                    message += ex.InnerException.Message;
                }
                MessageBox.Show(message);
            }
        }

        private void Encrypt_Click(object sender, EventArgs e)
        {
            try
            {
                var plain = PlainText.Text;
                var cipher = "";
                byte[] key = null;
                //
                var encryptedType = EncryptedType.SelectedIndex;
                switch (encryptedType)
                {
                    case 0:
                        if (!SecretKey.Text.IsNullOrEmptyOrWhiteSpace())
                        {
                            key = Convert.FromBase64String(SecretKey.Text);
                        }
                        cipher = DataEncryptor.EncryptToString(plain, key, DataEncryptorType.Normal);
                        break;
                    case 1:
                        if (!SecretKey.Text.IsNullOrEmptyOrWhiteSpace())
                        {
                            key = Convert.FromBase64String(SecretKey.Text);
                        }
                        cipher = DataEncryptor.EncryptToString(plain, key, DataEncryptorType.SM4);
                        break;
                    case 2:
                        if (!SecretKey.Text.IsNullOrEmptyOrWhiteSpace())
                        {
                            var keys = SecretKey.Text.Split(';');
                            key = Convert.FromBase64String(keys[0]);
                        }
                        cipher = DataEncryptor.EncryptToString(plain, key, DataEncryptorType.SM2);
                        break;
                    case 3:
                        if (!SecretKey.Text.IsNullOrEmptyOrWhiteSpace())
                        {
                            key = Convert.FromBase64String(SecretKey.Text);
                        }
                        cipher = DataEncryptor.EncryptToString(plain, key, DataEncryptorType.AES);
                        break;
                    case 4:
                        if (!SecretKey.Text.IsNullOrEmptyOrWhiteSpace())
                        {
                            var keys = SecretKey.Text.Split(';');
                            key = Convert.FromBase64String(keys[0]);
                        }
                        cipher = DataEncryptor.EncryptToString(plain, key, DataEncryptorType.RSA);
                        break;
                }
                CipherText.Text = cipher;
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                if (ex.InnerException != null)
                {
                    message += ex.InnerException.Message;
                }
                MessageBox.Show(message);
            }
        }

        private void Decrypt_Click(object sender, EventArgs e)
        {
            try
            {
                var plain = "";
                var cipher = CipherText.Text;
                byte[] key = null;
                //
                var encryptedType = EncryptedType.SelectedIndex;
                switch (encryptedType)
                {
                    case 0:
                        if (!SecretKey.Text.IsNullOrEmptyOrWhiteSpace())
                        {
                            key = Convert.FromBase64String(SecretKey.Text);
                        }
                        plain = DataEncryptor.DecryptFromString(cipher, key, DataEncryptorType.Normal);
                        break;
                    case 1:
                        if (!SecretKey.Text.IsNullOrEmptyOrWhiteSpace())
                        {
                            key = Convert.FromBase64String(SecretKey.Text);
                        }
                        plain = DataEncryptor.DecryptFromString(cipher, key, DataEncryptorType.SM4);
                        break;
                    case 2:
                        if (!SecretKey.Text.IsNullOrEmptyOrWhiteSpace())
                        {
                            var keys = SecretKey.Text.Split(';');
                            key = Convert.FromBase64String(keys[1]);
                        }
                        plain = DataEncryptor.DecryptFromString(cipher, key, DataEncryptorType.SM2);
                        break;
                    case 3:
                        if (!SecretKey.Text.IsNullOrEmptyOrWhiteSpace())
                        {
                            key = Convert.FromBase64String(SecretKey.Text);
                        }
                        plain = DataEncryptor.DecryptFromString(cipher, key, DataEncryptorType.AES);
                        break;
                    case 4:
                        if (!SecretKey.Text.IsNullOrEmptyOrWhiteSpace())
                        {
                            var keys = SecretKey.Text.Split(';');
                            key = Convert.FromBase64String(keys[1]);
                        }
                        plain = DataEncryptor.DecryptFromString(cipher, key, DataEncryptorType.RSA);
                        break;
                }
                PlainText.Text = plain;
            }
            catch (Exception ex)
            {
                var message = ex.Message;
                if (ex.InnerException != null)
                {
                    message += ex.InnerException.Message;
                }
                MessageBox.Show(message);
            }
        }
    }
}
